<?php

class Banner_model extends CI_Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();
			
		$this->_table = $this->config->item('database_tables');
	}

	// Public methods
	public function get_links($lang_id=null)
	{
		$this->db->select('*');
		$this->db->where('type',1);	
		if($lang_id != null)
		{
			$this->db->where('lang_id',$lang_id);
		}
		$this->db->order_by('order', 'ASC');
		$query = $this->db->get('banner');
			
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
	}
	public function get_all_banner()
	{
		$this->db->select('*');		
		$this->db->where('type',2);	
		$this->db->order_by('order', 'DESC');
		$query = $this->db->get('site_banner');
			
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
	}
	public function get_banner($id)
	{
		$this->db->select('*');
		$this->db->where('id', $id);
			
		$query = $this->db->get('site_banner', 1);
			
		if ($query->num_rows() == 1)
		{
			return $query->row();
		}
	}
	public function add_banner($photo=null)
	{
		$data = array
				(
					'name' => $this->input->post('name'),
					'url' => $this->input->post('url'),
					'target' => $this->input->post('target'),
					'type'=>2,
					'image'=>($photo) ? $photo['file_name']:'',
					'alt'=>'',
					'order'=>$this->get_last_order() + 1,
					'description' => $this->input->post('description'),
					'status' => $this->input->post('visible'),
				);
		$this->db->insert('site_banner', $data);
	}
	
	public function edit_banner($id)
	{
		$status = ($this->input->post('visible'))? 1:0;
		$data = array
				(
					'name' => $this->input->post('name'),
					'url' => $this->input->post('url'),
					'target' => $this->input->post('target'),					
					'alt'=>'',					
					'description' => $this->input->post('description'),
					'status' => $status
				);
			//var_dump($data);die();
		$this->db->where('id', $id);
		$this->db->update('site_banner', $data);
	}
	
	public function edit_banner2($id,$photo=null)
	{
		$status = ($this->input->post('visible'))? 1:0;
		$data = array
				(
					'name' => $this->input->post('name'),
					'url' => $this->input->post('url'),
					'target' => $this->input->post('target'),					
					'image'=>($photo) ? $photo['file_name']:'',
					'alt'=>'',					
					'description' => $this->input->post('description'),
					'status' => $status
				);
			//var_dump($data);die();
		$this->db->where('id', $id);
		$this->db->update('site_banner', $data);
	}

	public function delete_banner($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('site_banner');
		$this->reorganize_navigation();
	}

	protected function get_last_order()
	{
		$this->db->select('MAX(site_banner.order) AS pos');
		
		$query = $this->db->get('site_banner', 1);
			
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			
			return $result['pos'];
		}
	}

	public function reorganize_navigation()
	{
		$this->db->select('id');
		
		$query = $this->db->get('site_banner');
			
		if ($query->num_rows() > 0)
		{
			$result =  $query->result_array();
			
			$i = 0;

			foreach ($result as $row)
			{
				$this->db->set('order', ++$i);
				$this->db->where('id', $row['id']);
				$this->db->update('site_banner');
			}
		}
	}
	
	public function change_status_banner($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('site_banner',$data);		
	}
}

/* End of file links_model.php */
/* Location: ./application/modules/admin/models/links_model.php */